package com.quanyan.place.service.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.quanyan.api.APIResponse;
import com.quanyan.common.utils.DateTimeUtils;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.*;
import com.quanyan.place.entity.apireq.ReqSurveyPlace;
import com.quanyan.place.entity.apireq.ReqSurveyPlaceBaseInfo;
import com.quanyan.place.entity.apireq.ReqSurveyPlaceExtInfo;
import com.quanyan.place.entity.apiresp.RespSurveyPlace;
import com.quanyan.place.entity.apiresp.RespSurveyPlaceBaseInfo;
import com.quanyan.place.entity.apiresp.RespSurveyPlaceExtInfo;
import com.quanyan.place.entity.base.apiresp.RespDistrict;
import com.quanyan.place.mapper.*;
import com.quanyan.place.service.ApiPlaceSurveyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by youxiaojia on 2017/2/21.
 */
@Service
public class ApiPlaceSurveyServiceImpl implements ApiPlaceSurveyService {
    private static final Logger logger = LoggerFactory.getLogger(ApiPlaceSurveyServiceImpl.class);
    @Autowired
    VPlaceMapper vPlaceMapper;
    @Autowired
    TbDistrictMapper tbDistrictMapper;
    @Autowired
    TbCityMapper tbCityMapper;
    @Autowired
    TbBizPlaceMapper tbBizPlaceMapper;
    @Autowired
    TbBizPlaceSurveyMapper tbBizPlaceSurveyMapper;
    @Autowired
    TbBizPlaceSurveyCategoryMapper tbBizPlaceSurveyCategoryMapper;

    @Override
    public APIResponse<?> getDistrictListByCityId(Integer cityId) {
        List<RespDistrict> respDistrictList = new ArrayList<RespDistrict>();
        //查询地区列表
        TbDistrictExample tbDistrictExample = new TbDistrictExample();
        tbDistrictExample.createCriteria().andCityIdEqualTo(cityId);
        List<TbDistrict> tbDistricts = tbDistrictMapper.selectByExample(tbDistrictExample);
        if (CollectionUtils.isEmpty(tbDistricts)) {
            //do nothing
        }
        TbCity tbCity = tbCityMapper.selectByPrimaryKey(cityId);
        if (tbCity == null){
            return APIResponse.returnFail("没有该城市");
        } else {
            RespDistrict allDistrict = new RespDistrict();//添加全城子项
            allDistrict.setCityId(tbCity.getId());
            allDistrict.setId(0);//默认区域Id为0
            allDistrict.setName("全" + tbCity.getName());
            respDistrictList.add(0, allDistrict);//全城置顶
        }
        for (TbDistrict tbDistrict : tbDistricts){
            RespDistrict respDistrict = new RespDistrict();
            BeanUtils.copyProperties(tbDistrict, respDistrict);
            respDistrictList.add(respDistrict);
        }
        return APIResponse.returnSuccess(respDistrictList);
    }

    @Override
    public APIResponse<?> getSurveyPlaceListByCondition(ReqSurveyPlace reqSurveyPlace) {
        List<RespSurveyPlace> respSurveyPlaceList  =  vPlaceMapper.getSurveyPlaceListByCondition(reqSurveyPlace);
        return APIResponse.returnSuccess(respSurveyPlaceList);
    }

    @Override
    public APIResponse<?> getSurveyPlaceBaseInfo(Integer placeId) {
        if (placeId == null){
            return APIResponse.returnFail("场馆id必传");
        }
        List<RespSurveyPlaceBaseInfo> respSurveyPlaceBaseInfoList = vPlaceMapper.getSurveyPlaceBaseInfo(placeId);
        if (CollectionUtils.isEmpty(respSurveyPlaceBaseInfoList)){
            return APIResponse.returnFail("没有该场馆基本信息");
        }
        return APIResponse.returnSuccess(respSurveyPlaceBaseInfoList.get(0));
    }

    @Override
    public APIResponse<?> submitSurveyPlaceBaseInfo(ReqSurveyPlaceBaseInfo reqSurveyPlaceBaseInfo) {
        TbBizPlace tbBizPlace = tbBizPlaceMapper.selectByPrimaryKey(reqSurveyPlaceBaseInfo.getPlaceId());
        if (tbBizPlace == null){
            return APIResponse.returnFail("场馆已被删除");
        }
        if (reqSurveyPlaceBaseInfo.getPlacePhone() != null){
            if (reqSurveyPlaceBaseInfo.getPlacePhone().length() > 50){
                return APIResponse.returnFail("电话最多不能超过50个数字");
            }
        }
        tbBizPlace.setName(reqSurveyPlaceBaseInfo.getPlaceName());
        tbBizPlace.setAddress(reqSurveyPlaceBaseInfo.getPlaceAddress());
        tbBizPlace.setPhone(reqSurveyPlaceBaseInfo.getPlacePhone());
        tbBizPlace.setAvailableTimeStart(reqSurveyPlaceBaseInfo.getAvailableTimeStart());
        tbBizPlace.setAvailableTimeEnd(reqSurveyPlaceBaseInfo.getAvailableTimeEnd());
        tbBizPlaceMapper.updateByPrimaryKey(tbBizPlace);
        TbBizPlaceSurveyExample tbBizPlaceSurveyExample = new TbBizPlaceSurveyExample();
        tbBizPlaceSurveyExample.createCriteria().andPlaceIdEqualTo(reqSurveyPlaceBaseInfo.getPlaceId());
        List<TbBizPlaceSurvey> tbBizPlaceSurveys = tbBizPlaceSurveyMapper.selectByExample(tbBizPlaceSurveyExample);

        if (CollectionUtils.isEmpty(tbBizPlaceSurveys)){
            //add
            TbBizPlaceSurvey tbBizPlaceSurvey = new TbBizPlaceSurvey();
            BeanUtils.copyProperties(reqSurveyPlaceBaseInfo,tbBizPlaceSurvey);
            if (reqSurveyPlaceBaseInfo.getIsEffective().intValue() == 0){
                tbBizPlaceSurvey.setImproveStatus(PlaceConstants.YES.intValue());
            }
            tbBizPlaceSurveyMapper.insertSelective(tbBizPlaceSurvey);
        } else {
            if (tbBizPlaceSurveys.size()> 1){
                return APIResponse.returnFail("存在多个场馆基本信息");
            } else {
                //update
                TbBizPlaceSurvey tbBizPlaceSurvey = tbBizPlaceSurveys.get(0);
                BeanUtils.copyProperties(reqSurveyPlaceBaseInfo,tbBizPlaceSurvey);
                if (reqSurveyPlaceBaseInfo.getIsEffective().intValue() == 0){
                    tbBizPlaceSurvey.setImproveStatus(PlaceConstants.YES.intValue());
                }
                tbBizPlaceSurveyMapper.updateByPrimaryKey(tbBizPlaceSurvey);
            }
        }
        return APIResponse.returnSuccess();
    }

    @Override
    public APIResponse<?> getSurveyPlaceExtInfo(Integer placeId) {
        TbBizPlaceSurveyCategoryExample tbBizPlaceSurveyCategoryExample = new TbBizPlaceSurveyCategoryExample();
        tbBizPlaceSurveyCategoryExample.createCriteria().andPlaceIdEqualTo(placeId);
        List<TbBizPlaceSurveyCategory> tbBizPlaceSurveyCategoryList = tbBizPlaceSurveyCategoryMapper.selectByExample(tbBizPlaceSurveyCategoryExample);
        List<RespSurveyPlaceExtInfo> respSurveyPlaceExtInfoList = tbBizPlaceSurveyCategory2RespSurveyPlaceExtInfo(tbBizPlaceSurveyCategoryList);
        return APIResponse.returnSuccess(respSurveyPlaceExtInfoList);
    }

    @Override
    public APIResponse<?> submitSurveyPlaceExtInfo(ReqSurveyPlaceExtInfo reqSurveyPlaceExtInfo) {
        BigDecimal one = new BigDecimal(1);
        BigDecimal nine = new BigDecimal(999);
        if (reqSurveyPlaceExtInfo.getWeekdayIdlerPrice() != null){
            if (one.compareTo(reqSurveyPlaceExtInfo.getWeekdayIdlerPrice()) == 1
                    || nine.compareTo(reqSurveyPlaceExtInfo.getWeekdayIdlerPrice()) == -1){//小于1 大于999
                return APIResponse.returnFail("价格只能输入1-999区间");
            }
        }
        if (reqSurveyPlaceExtInfo.getWeekdayBusyPrice() != null){
            if (one.compareTo(reqSurveyPlaceExtInfo.getWeekdayBusyPrice()) == 1
                    || nine.compareTo(reqSurveyPlaceExtInfo.getWeekdayBusyPrice()) == -1){//小于1 大于999
                return APIResponse.returnFail("价格只能输入1-999区间");
            }
        }
        if (reqSurveyPlaceExtInfo.getWeekendIdlerPrice() != null){
            if (one.compareTo(reqSurveyPlaceExtInfo.getWeekendIdlerPrice()) == 1
                    || nine.compareTo(reqSurveyPlaceExtInfo.getWeekendIdlerPrice()) == -1){//小于1 大于999
                return APIResponse.returnFail("价格只能输入1-999区间");
            }
        }
        if (reqSurveyPlaceExtInfo.getWeekendBusyPrice() != null){
            if (one.compareTo(reqSurveyPlaceExtInfo.getWeekendBusyPrice()) == 1
                    || nine.compareTo(reqSurveyPlaceExtInfo.getWeekendBusyPrice()) == -1){//小于1 大于999
                return APIResponse.returnFail("价格只能输入1-999区间");
            }
        }
        TbBizPlaceSurveyCategory tbBizPlaceSurveyCategory = new TbBizPlaceSurveyCategory();
        BeanUtils.copyProperties(reqSurveyPlaceExtInfo,tbBizPlaceSurveyCategory);

        tbBizPlaceSurveyCategory.setWeekdayIdlerStartTime(DateTimeUtils.convertString2Date("HH:mm",reqSurveyPlaceExtInfo.getWeekdayIdlerStartTime()));
        tbBizPlaceSurveyCategory.setWeekdayIdlerEndTime(DateTimeUtils.convertString2Date("HH:mm",reqSurveyPlaceExtInfo.getWeekdayIdlerEndTime()));
        tbBizPlaceSurveyCategory.setWeekdayBusyStartTime(DateTimeUtils.convertString2Date("HH:mm",reqSurveyPlaceExtInfo.getWeekdayBusyStartTime()));
        tbBizPlaceSurveyCategory.setWeekdayBusyEndTime(DateTimeUtils.convertString2Date("HH:mm",reqSurveyPlaceExtInfo.getWeekdayBusyEndTime()));
        tbBizPlaceSurveyCategory.setWeekendIdlerStartTime(DateTimeUtils.convertString2Date("HH:mm",reqSurveyPlaceExtInfo.getWeekendIdlerStartTime()));
        tbBizPlaceSurveyCategory.setWeekendIdlerEndTime(DateTimeUtils.convertString2Date("HH:mm",reqSurveyPlaceExtInfo.getWeekendIdlerEndTime()));
        tbBizPlaceSurveyCategory.setWeekendBusyStartTime(DateTimeUtils.convertString2Date("HH:mm",reqSurveyPlaceExtInfo.getWeekendBusyStartTime()));
        tbBizPlaceSurveyCategory.setWeekendBusyEndTime(DateTimeUtils.convertString2Date("HH:mm",reqSurveyPlaceExtInfo.getWeekendBusyEndTime()));

        try {
            tbBizPlaceSurveyCategoryMapper.insertSelective(tbBizPlaceSurveyCategory);
        }catch (Exception e){
            logger.warn("存在调研场馆的运动类别--更新操作");
            TbBizPlaceSurveyCategoryExample tbBizPlaceSurveyCategoryExample = new TbBizPlaceSurveyCategoryExample();
            tbBizPlaceSurveyCategoryExample.createCriteria().andPlaceIdEqualTo(reqSurveyPlaceExtInfo.getPlaceId())
                    .andCategoryIdEqualTo(reqSurveyPlaceExtInfo.getCategoryId());
            tbBizPlaceSurveyCategoryMapper.updateByExampleSelective(tbBizPlaceSurveyCategory,tbBizPlaceSurveyCategoryExample);
        }
        TbBizPlaceSurveyExample tbBizPlaceSurveyExample = new TbBizPlaceSurveyExample();
        tbBizPlaceSurveyExample.createCriteria().andPlaceIdEqualTo(reqSurveyPlaceExtInfo.getPlaceId());
        TbBizPlaceSurvey tbBizPlaceSurvey = new TbBizPlaceSurvey();
        tbBizPlaceSurvey.setImproveStatus(PlaceConstants.YES.intValue());
        tbBizPlaceSurveyMapper.updateByExampleSelective(tbBizPlaceSurvey,tbBizPlaceSurveyExample);
        return APIResponse.returnSuccess();
    }

    private List<RespSurveyPlaceExtInfo> tbBizPlaceSurveyCategory2RespSurveyPlaceExtInfo(List<TbBizPlaceSurveyCategory> tbBizPlaceSurveyCategoryList){
        List<RespSurveyPlaceExtInfo> respSurveyPlaceExtInfos = new ArrayList<RespSurveyPlaceExtInfo>();
        for (TbBizPlaceSurveyCategory tbBizPlaceSurveyCategory : tbBizPlaceSurveyCategoryList){
            RespSurveyPlaceExtInfo respSurveyPlaceExtInfo =  new RespSurveyPlaceExtInfo();
            BeanUtils.copyProperties(tbBizPlaceSurveyCategory,respSurveyPlaceExtInfo);

            respSurveyPlaceExtInfo.setWeekdayIdlerStartTime(DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceSurveyCategory.getWeekdayIdlerStartTime()));
            respSurveyPlaceExtInfo.setWeekdayIdlerEndTime(DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceSurveyCategory.getWeekdayIdlerEndTime()));
            respSurveyPlaceExtInfo.setWeekdayBusyStartTime(DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceSurveyCategory.getWeekdayBusyStartTime()));
            respSurveyPlaceExtInfo.setWeekdayBusyEndTime(DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceSurveyCategory.getWeekdayBusyEndTime()));
            respSurveyPlaceExtInfo.setWeekendIdlerStartTime(DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceSurveyCategory.getWeekendIdlerStartTime()));
            respSurveyPlaceExtInfo.setWeekendIdlerEndTime(DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceSurveyCategory.getWeekendIdlerEndTime()));
            respSurveyPlaceExtInfo.setWeekendBusyStartTime(DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceSurveyCategory.getWeekendBusyStartTime()));
            respSurveyPlaceExtInfo.setWeekendBusyEndTime(DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceSurveyCategory.getWeekendBusyEndTime()));

            respSurveyPlaceExtInfos.add(respSurveyPlaceExtInfo);
        }
        return respSurveyPlaceExtInfos;
    }
}
